﻿Imports System.Xml
''' <summary>
''' 对于COPY句的整理
''' </summary>
''' <remarks>式样书：COPY句的处理思考-1,2</remarks>
Public Class clsCopyBook
    ''' <summary>
    ''' COPY句整理
    ''' </summary>
    ''' <param name="CopyBookPath">COPY句路劲</param>
    ''' <remarks></remarks>
    Public Shared Sub GenerateXML(ByVal CopyBookPath As String)
        'COPY句第一个元素和最后元素的层号
        Dim FirstLayNo As Byte = 0
        Dim LastLayNo As Byte = 0
        Dim CopyBookRoot As XmlElement
        Dim ProcessLine As Integer = 1
        Dim DataList As List(Of XmlElement)
        mXmlDocument = New XmlDocument
        CopyBookRoot = mXmlDocument.CreateElement("Copybook")
        'TODO:1 缺少分号的问题。
        '     2 INCLUDE的递归
        '读取所有行
        ReadFileToPL1Source(CopyBookPath)
        '将COPY句整理成为单句
        FormatPL1Source()
        '分析指定行
        Do While ProcessLine <= PL1Source.Count
            Dim SingleLineWithOutComment As String = PL1Source(ProcessLine - 1)
            '去掉注释
            If InStr(SingleLineWithOutComment, "*/") > 0 Then
                SingleLineWithOutComment = ClearComment(SingleLineWithOutComment)
            End If
            '数据定义部判断DCL开始
            Select Case True
                Case SingleLineWithOutComment.StartsWith("DCL" & Space(1))
                    DataList = clsDataDefine.GetDataTree(SingleLineWithOutComment)
                    For Each SingleData As XmlElement In DataList
                        CopyBookRoot.AppendChild(SingleData)
                    Next
                    If FirstLayNo = 0 Then
                        FirstLayNo = 1
                    End If
                    LastLayNo = GetLastLayNo(DataList(DataList.Count - 1))
                Case SingleLineWithOutComment = String.Empty
                    Dim CommentNode As XmlElement = mXmlDocument.CreateElement("COMMENTLIST")
                    CommentNode.SetAttribute("TEXT", PL1Source(ProcessLine - 1))
                    CopyBookRoot.AppendChild(CommentNode)
                Case Else
                    '手工添加一个DCL 1 DUMMY,的数据顶层，然后取得数据
                    SingleLineWithOutComment = "DCL 1 DUMMY," & SingleLineWithOutComment
                    DataList = clsDataDefine.GetDataTree(SingleLineWithOutComment)
                    '必须使用一个Clone对象，AppendChild的时候，会将原来的节点移除掉
                    For i As Integer = 0 To DataList.Count - 1
                        CopyBookRoot.AppendChild(DataList(i))
                    Next
                    If FirstLayNo = 0 Then
                        FirstLayNo = GetFirstLayNo(DataList(0))
                    End If
                    LastLayNo = GetLastLayNo(DataList(DataList.Count - 1))
            End Select
            ProcessLine += 1
        Loop
        With CType(CopyBookRoot, XmlElement)
            .SetAttribute("FirstLayNo", FirstLayNo)
            .SetAttribute("LastLayNo", LastLayNo)
        End With

        Dim copyBookName As String = IO.Path.GetFileNameWithoutExtension(CopyBookPath)
        If mCopyBookInfoDic.ContainsKey(copyBookName) Then
            mCopyBookInfoDic.Remove(copyBookName)
        End If
        Dim copyRec As CopyBookInfoRec
        copyRec.FirstLayNo = FirstLayNo
        copyRec.LastLayNo = LastLayNo
        copyRec.CopyBookType = enmCopyBookType.Data
        copyRec.IsDisposed = False
        copyRec.CopyBookName = copyBookName
        mCopyBookInfoDic.Add(copyBookName, copyRec)
        mXmlDocument.AppendChild(CopyBookRoot)
        mXmlDocument.Save(MyConfig.CopyXMLFolder & copyBookName & ".xml")
    End Sub
    ''' <summary>
    ''' 获取数据结构的最后一个层号
    ''' </summary>
    ''' <param name="TestNode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Shared Function GetFirstLayNo(ByVal TestNode As XmlNode) As Byte
        '获得最后一个元素的层号
        Dim rtnLayNo As Byte
        Dim FirstNode As XmlElement = TestNode
        If FirstNode.Name = "INCLUDE" Then
            'TODO假设已经生成了XML文档
            Return mCopyBookInfoDic(CType(TestNode, XmlElement).GetAttribute("COPYBOOKNAME")).FirstLayNo
        End If
        rtnLayNo = CType(FirstNode, XmlElement).GetAttribute("LayNo")
        Return rtnLayNo
    End Function
    ''' <summary>
    ''' 获取数据结构的最后一个层号
    ''' </summary>
    ''' <param name="TestNode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Shared Function GetLastLayNo(ByVal TestNode As XmlNode) As Byte
        '获得最后一个元素的层号
        Dim rtnLayNo As Byte
        Dim LastNode As XmlElement = TestNode
        If LastNode.Name = "INCLUDE" Then
            'TODO假设已经生成了XML文档
            Return mCopyBookInfoDic(CType(LastNode, XmlElement).GetAttribute("COPYBOOKNAME")).LastLayNo
        End If
        Do While LastNode.HasChildNodes
            LastNode = LastNode.LastChild
        Loop
        If LastNode.Name = "INCLUDE" Then
            'TODO假设已经生成了XML文档
            Return mCopyBookInfoDic(CType(LastNode, XmlElement).GetAttribute("COPYBOOKNAME")).LastLayNo
        End If
        rtnLayNo = CType(LastNode, XmlElement).GetAttribute("LayNo")
        Return rtnLayNo
    End Function
    ''' <summary>
    ''' 为COPY句节点设置属性
    ''' </summary>
    ''' <param name="XmlNode"></param>
    ''' <returns>是否获取成功</returns>
    ''' <remarks></remarks>
    Public Shared Function SetCopyBookByCopyBookInfoDic(ByVal XmlNode As XmlElement) As Boolean
        '通过COPYBOOK一览表来获取
        If mCopyBookInfoDic.ContainsKey(XmlNode.GetAttribute("COPYBOOKNAME")) Then
            With mCopyBookInfoDic(XmlNode.GetAttribute("COPYBOOKNAME"))
                XmlNode.SetAttribute("FirstLayNo", .FirstLayNo)
                XmlNode.SetAttribute("LastLayNo", .LastLayNo)
            End With
            Return True
        End If
    End Function
End Class
